//
//  SNHStaticTableViewInfo.m
//  Pods
//
//  Created by 陈建蕾 on 2018/6/9.
//

#import "SNHStaticTableViewInfo.h"
#import "SNHStaticTableViewCell.h"

@interface SNHStaticTableViewInfo ()
<
    UITableViewDelegate,
    UITableViewDataSource
>
/** sections */
@property (nonatomic, strong) NSMutableArray *arraySections;
@end

@implementation SNHStaticTableViewInfo {
    SNHStaticTableView *_tableView;
}

- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super init]) {
        _tableView = [[SNHStaticTableView alloc] initWithFrame:frame style:style];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _arraySections = [NSMutableArray array];
    }
    return self;
}

- (void)addSection:(SNHStaticTableViewSectionInfo *)section {
    [self.arraySections addObject:section];
}

- (void)insertSection:(SNHStaticTableViewSectionInfo *)section atIndex:(NSInteger)index {
    [self.arraySections insertObject:section atIndex:index];
}

- (void)removeSectionAtIndex:(NSInteger)index {
    [self.arraySections removeObjectAtIndex:index];
}

- (void)removeCell:(NSIndexPath *)indexPath {
    if (indexPath.section >= self.arraySections.count) return;
    SNHStaticTableViewSectionInfo *section = self.arraySections[indexPath.section];
    [section removeCellAtIndex:indexPath.row];
}

- (void)removeAllSection {
    [self.arraySections removeAllObjects];
}

- (SNHStaticTableViewSectionInfo *)getSectionAtIndex:(NSInteger)index {
    if (index >= self.arraySections.count) return nil;
    return self.arraySections[index];
}

- (SNHStaticTableViewCellInfo *)getCellAtRow:(NSInteger)row section:(NSInteger)section {
    if (section >= self.arraySections.count) return nil;
    SNHStaticTableViewSectionInfo *sectionInfo = self.arraySections[section];
    SNHStaticTableViewCellInfo *cellInfo = [sectionInfo getCellInfoAtIndex:row];
    return cellInfo;
}

- (NSInteger)sectionCount {
    return self.arraySections.count;
}

- (SNHStaticTableView *)tableView {
    return _tableView;
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.arraySections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section >= self.arraySections.count) return 0;
    SNHStaticTableViewSectionInfo *sectionInfo = self.arraySections[section];
    return [sectionInfo rowCount];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section >= self.arraySections.count) return 0;
    SNHStaticTableViewSectionInfo *sectionInfo = self.arraySections[indexPath.section];
    SNHStaticTableViewCellInfo *cellInfo = [sectionInfo getCellInfoAtIndex:indexPath.row];
    return cellInfo.height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section >= self.arraySections.count) return 0;
    SNHStaticTableViewSectionInfo *sectionInfo = self.arraySections[section];
    return sectionInfo.headerHeight;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section >= self.arraySections.count) return 0;
    SNHStaticTableViewSectionInfo *sectionInfo = self.arraySections[section];
    return sectionInfo.footerHeight;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    if (section >= self.arraySections.count) return nil;
    SNHStaticTableViewSectionInfo *sectionInfo = self.arraySections[section];
    return [sectionInfo getUserInfoForKey:@"headerTitle"];
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    if (section >= self.arraySections.count) return nil;
    SNHStaticTableViewSectionInfo *sectionInfo = self.arraySections[section];
    return [sectionInfo getUserInfoForKey:@"footerTitle"];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section >= self.arraySections.count) return nil;
    SNHStaticTableViewSectionInfo *sectionInfo = self.arraySections[section];
    return [sectionInfo getUserInfoForKey:@"headerView"];
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    if (section >= self.arraySections.count) return nil;
    SNHStaticTableViewSectionInfo *sectionInfo = self.arraySections[section];
    return [sectionInfo getUserInfoForKey:@"footerView"];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section >= self.arraySections.count) return nil;
    SNHStaticTableViewSectionInfo *sectionInfo = self.arraySections[indexPath.section];
    if (indexPath.row >= [sectionInfo rowCount]) return nil;
    SNHStaticTableViewCellInfo *cellInfo = [sectionInfo getCellInfoAtIndex:indexPath.row];
    NSString *identifier = [NSString stringWithFormat:@"SNHStaticTableViewCell_%zd_%zd", indexPath.section, indexPath.row];
    SNHStaticTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[SNHStaticTableViewCell alloc] initWithStyle:cellInfo.cellStyle reuseIdentifier:identifier];
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        if ([cellInfo.makeTarget respondsToSelector:cellInfo.makeAction]) {
            [cellInfo.makeTarget performSelector:cellInfo.makeAction withObject:cell withObject:cellInfo];
        }
#pragma clang diagnostic pop
    }
    cell.accessoryType = cellInfo.accessoryType;
    cell.selectionStyle = cellInfo.selectionStyle;
    cell.textLabel.text = [cellInfo getUserInfoForKey:@"title"];
    cell.detailTextLabel.text = [cellInfo getUserInfoForKey:@"rightValue"];
    NSString *imagName = [cellInfo getUserInfoForKey:@"imageName"];
    if (imagName.length > 0) {
        cell.imageView.image = [UIImage imageNamed:imagName];
    }
    return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section >= self.arraySections.count) return;
    SNHStaticTableViewSectionInfo *sectionInfo = self.arraySections[indexPath.section];
    if (indexPath.row >= [sectionInfo rowCount]) return;
    SNHStaticTableViewCellInfo *cellInfo = [sectionInfo getCellInfoAtIndex:indexPath.row];
    id target = cellInfo.target;
    SEL action = cellInfo.action;
    if ([target respondsToSelector:action]) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
        [target performSelector:action withObject:cellInfo withObject:indexPath];
#pragma clang diagnostic pop
    }
}

#pragma mark - Private
- (NSMutableArray *)arraySections {
    if (!_arraySections) {
        _arraySections = [NSMutableArray array];
    }
    return _arraySections;
}
@end
